package com.wuliupai.delivery.wuliupaidelivery.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.bean.MissionDateInfo;
import com.wuliupai.delivery.wuliupaidelivery.bean.MissionStatisticsBean;
import com.wuliupai.delivery.wuliupaidelivery.utils.NumberUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.StringUtils;

import java.util.List;

/**
 * Descriptions：任务统计详情中列表的Adapter
 * <p>
 * Author：ChenME
 * Date：2017/1/7
 * Email：ibelieve1210@163.com
 */
public class MissionStatisticsDetailListAdapter extends BaseAdapter {

    private List<MissionDateInfo> dataList;
    private Context context;
    private int whichPage;

    private int baseKPI = 1;

    public MissionStatisticsDetailListAdapter(Context context) {
        this.context = context;
    }

    public void setBaseKPI(int baseKPI) {
        this.baseKPI = baseKPI;
    }

    /**
     * 设置当前页面类型
     *
     * @param whichPage
     */
    public void setPage(int whichPage) {
        this.whichPage = whichPage;
    }

    public void setData(List<MissionDateInfo> dataList) {
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return null != dataList ? dataList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_mission_statistics_detail_lv, null);
            holder.tv_date = (TextView) convertView.findViewById(R.id.tv_date);
            holder.tv_complete = (TextView) convertView.findViewById(R.id.tv_complete);
            holder.tv_totalAndPercent = (TextView) convertView.findViewById(R.id.tv_totalAndPercent);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        try {
            MissionDateInfo bean = dataList.get(position);
            holder.tv_date.setText(bean.date);

            String percentStr = StringUtils.dropNeedlessZeroOfNum(
                    NumberUtils.formatDouble(((bean.money * 1f) / (baseKPI * 1f)) * 100, "0.00") + "") + "%";

            String totalAndPercent = "";
            if (MissionStatisticsBean.Type_Year == whichPage) {
                holder.tv_complete.setText(
                        String.format(context.getString(R.string.string_missionStatisticsDetail_completeMonth),
                                StringUtils.dropNeedlessZeroOfNum((bean.money / 100) + "")));
                totalAndPercent = String.format(context.getString(R.string.string_missionStatisticsDetail_itemTotal),
                        StringUtils.dropNeedlessZeroOfNum((baseKPI / 100) + " / "));
            } else {
                holder.tv_complete.setText(
                        String.format(context.getString(R.string.string_missionStatisticsDetail_completeDay),

                                StringUtils.dropNeedlessZeroOfNum((bean.money / 100) + "")));
            }
            totalAndPercent += String.format(context.getString(R.string.string_missionStatisticsDetail_itemPercent), percentStr);
            holder.tv_totalAndPercent.setText(totalAndPercent);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    class ViewHolder {
        private TextView tv_date;
        private TextView tv_complete;
        private TextView tv_totalAndPercent;
    }
}